package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;

import com.srwl.mytx.Helper;
import com.srwl.mytx.R;
import com.srwl.mytx.RetrofitManager;
import com.srwl.mytx.activity.BaseActivity;
import com.srwl.mytx.domain.CommonResult;
import com.srwl.mytx.domain.Forward;
import com.srwl.mytx.service.CommentService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForwardCommentView extends CommentView {
    public ForwardCommentView(@NonNull Context context) {
        super(context);
    }

    public ForwardCommentView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ForwardCommentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void init(Forward forward, final InteractionListener listener) {
        this.listener = listener;
        this.topicId = forward.getfId();
        commentSize = forward.getCommentSize();
        if (commentSize == null) {
            commentSize = 0;
            tv_commentCount.setText("暂无评论");

        } else {
            tv_commentCount.setText("共 " + commentSize + " 条评论");
        }
        loadData(true);
    }

    /**
     * 提交一个普通评论
     */
    protected void commitNormalComment() {
        if (!Helper.getInstance().isNetworkConnected()) {
            ((BaseActivity) context).showToast_v1(context.getString(R.string.network_anomalies));
            return;
        }
        RetrofitManager.getInstance().createRequest(CommentService.class)
                .createComment_v1(targetComment)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {

                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }

                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }
                        commentSize++;
                        tv_commentCount.setText("共 " + commentSize + " 条评论");
                        String c_id = result.getData() + "";
                        targetComment.setId(c_id);
                        //把评论插到第一个的位置
                        commentList.add(0, targetComment);
                        commentAdapter.notifyDataSetChanged();
                        if (listener != null) {
                            listener.onCreateNormalCommentSuccess();
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        Log.e(TAG, "onFailure: " + t.getMessage());
                        if (listener != null) {

                            listener.showToast(context.getString(R.string.system_busy));
                        }
                    }
                });
    }

    protected void commitChildComment() {
        if (!Helper.getInstance().isNetworkConnected()) {
            ((BaseActivity) context).showToast_v1(context.getString(R.string.network_anomalies));
            return;
        }
        commentAdapter.setForbiddenItemClick(true);//添加子评论的时候，禁用 评论项的点击，避免位置错乱
        RetrofitManager.getInstance().createRequest(CommentService.class)
                .createForwardChildComment(targetComment)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {

                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }
                        commitChildCommentComplete();
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        Log.e(TAG, "onFailure: " + t.getMessage());
                        if (listener != null) {
                            listener.onCreateCommentError();
                        }
                        commentAdapter.setForbiddenItemClick(false);

                    }
                });
    }

}
